# -*- coding: utf-8 -*-

from dbobject import DBObject
from aeConn import dbHandle
from reaction import Reaction
from compound import EquCompound

import yaml

class Pathway(DBObject):
	def __init__(self, key=''):
		"""key is either the key or another Pathway
		"""
		if (type(key)==Pathway):
			DBObject.__init__(self, key)
			return
		else:
			DBObject.__init__(self)

		self.key = key

		# This is the graph of the pathway the form (edge list) is:
		# [Reaction1,(Reaction2,Compound)] which means
		# Reaction1 points to Reaction2 via Compound
		self._things['graph'] = []

		self._things['name'] = ""
		self._things['description'] = ''


	def __iter__(self):
		for edge in self._things['graph']:
			yield edge


	def _loadYAML(self, yml):
		data = yaml.load(yml)
		graph = []
		if "graph" in data.keys():
			for edge in data['graph']:
				graph.append([Reaction(edge[0]), (Reaction(edge[1][0]), EquCompound(edge[1][1]))])
			del data["graph"]
			self.graph = graph

		DBObject._loadYAML(self, data)

	def _dumpYAML(self, properties=[]):
		if properties:
			keys = properties
		else:
			keys = self.properties().keys()

		data = {}

		if "graph" in keys:
			keys.remove("graph")
			g = []
			for edge in self._things['graph']:
				g.append([edge[0].key, (edge[1][0].key, edge[1][1].key)])
			data["graph"] = g
		
		rest = list(set(keys) - set(data.keys()))
		return DBObject._dumpYAML(self, otherData=data, properties=rest)